package com.example.choupaiapp.main.fragment.message;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.choupaiapp.R;
import com.example.choupaiapp.common.util.DialogUtil;
import com.example.choupaiapp.common.util.SharedPreferencesHelp;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.main.activity.message.ChatActivity;
import com.example.choupaiapp.main.activity.message.ChouPaiActivity;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.ui.EaseConversationListFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class ConversationListFragment extends EaseConversationListFragment {


    public static ConversationListFragment newInstance() {
        return new ConversationListFragment();
    }


    @Override
    protected void initView() {
        super.initView();
        EventBus.getDefault().register(this);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void setUpView() {
        super.setUpView();

//        View headView = View.inflate(getActivity(), R.layout.item_conver_head, null);

        if (conversationListView.getAdapter().getCount() > 0) {
            empty_img.setVisibility(View.GONE);
        } else {
            empty_img.setVisibility(View.VISIBLE);
        }


        conversationListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (Utils.isDoubleClick()) {
                    return;
                }

                EMConversation conversation = conversationListView.getItem(position);

                conversation.markAllMessagesAsRead();
                EventBus.getDefault().post("conversationClear");

                String userid = conversation.conversationId();
                if (userid.equals(EMClient.getInstance().getCurrentUser())) {
                    Toast.makeText(getActivity(), R.string.Cant_chat_with_yourself, Toast.LENGTH_SHORT).show();
                } else if (userid.equalsIgnoreCase("HX_209")) {
                    Intent intent = new Intent(getActivity(), ChouPaiActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("userId", userid);
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    // start chat acitivity
                    Intent intent = new Intent(getActivity(), ChatActivity.class);
//                    if (conversation.isGroup()) {
//                        if (conversation.getType() == EMConversationType.ChatRoom) {
//                            // it's group chat
//                            intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_CHATROOM);
//                        } else {
//                            intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_GROUP);
//                        }
//                    }
                    // it's single chat
                    intent.putExtra(EaseConstant.EXTRA_USER_ID, userid);
                    startActivity(intent);
                }

            }
        });

        conversationListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {

                DialogUtil.getInstance().showCenterDialog(getActivity(), R.layout.dialog_message_more);
                LinearLayout dialog_message_more = (LinearLayout) DialogUtil.getInstance().dialog_center_layout;

                LinearLayout delete = dialog_message_more.findViewById(R.id.delete);

                delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (Utils.isDoubleClick()) {
                            return;
                        }
                        DialogUtil.getInstance().dialog_center.dismiss();
                        EMConversation conversation = conversationListView.getItem(i);
                        conversation.markAllMessagesAsRead();
                        EventBus.getDefault().post("conversationClear");
                        EMClient.getInstance().chatManager().deleteConversation(conversation.conversationId(), true);
                        SharedPreferencesHelp.remove(conversation.conversationId());
                        conversationList.remove(i);
                        conversationListView.refresh();
                        if (conversationListView.getAdapter().getCount() > 0) {
                            empty_img.setVisibility(View.GONE);
                        } else {
                            empty_img.setVisibility(View.VISIBLE);
                        }
                    }
                });
                return true;
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void OnMessageEvent(String event) {
        if (event.equals("conversationListCount")) {
            mark_ll.setVisibility(View.VISIBLE);

            mark_ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mark_ll.setVisibility(View.GONE);
                    EMClient.getInstance().chatManager().markAllConversationsAsRead();
                    EventBus.getDefault().post("conversationClear");
                }
            });
        } else if (event.equals("receive_msg")) {
            empty_img.setVisibility(View.GONE);
        }
    }

}